package com.xudu.tourbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.entity.Element;
import com.xudu.tourbackend.entity.Tag;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.mapper.ElementMapper;
import com.xudu.tourbackend.mapper.TagMapper;
import com.xudu.tourbackend.service.ITagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xudu.tourbackend.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xudu
 * @since 2024-06-22
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements ITagService {

    @Resource
    private TagMapper tagMapper;

    @Resource
    private ElementMapper elementMapper;

    @Resource
    private IUserService userService;

    @Override
    public Integer addTag(String tagName, HttpServletRequest request) {
        if (StringUtils.isBlank(tagName)){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }
        //判断标签名称是否重复
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Tag::getTagName, tagName);
        if (tagMapper.selectCount(queryWrapper) > 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR, "标签名称重复");
        }

        Tag tag = new Tag();
        tag.setTagName(tagName);

        //boolean save = this.save(tag);
        //if (!save){
        //    return null;
        //}
        int insert = tagMapper.insert(tag);
        if (insert <= 0){
            throw new ServiceException(ErrorCode.SYSTEM_ERROR);
        }
        return tag.getTagId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class) //操作多个数据库表的时候防止个别数据库表操作失败，一起成功或者一起失败
    public int deleteTag(List<Integer> ids, HttpServletRequest request) {
        for (Integer id : ids){
            if (id == null || id <= 0){
                throw new ServiceException(ErrorCode.PARAMS_ERROR);
            }
        }

        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }
        int i = tagMapper.deleteBatchIds(ids);
        //关联删除tagid对应的元素数据
        for (Integer id : ids){
            QueryWrapper<Element> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Element::getTagId, id);
            elementMapper.delete(queryWrapper);
        }
        if (i <= 0){
            throw new ServiceException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return i;

    }

    @Override
    public Boolean updateTag(Integer id, String tagName, HttpServletRequest request) {
        if (id == null || id <= 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        if (StringUtils.isBlank(tagName)){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }

        Tag tagdb = tagMapper.selectById(id);
        if (tagdb == null){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        Tag tag = new Tag();
        tag.setTagId(id);
        tag.setTagName(tagName);

        boolean b = this.updateById(tag);
        if (!b){
            throw new ServiceException(ErrorCode.SYSTEM_ERROR);
        }
        return b;

    }

    @Override
    public Tag searchTag(int id, HttpServletRequest request) {
        if (id <= 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        if (!userService.isAdmin(request)){
            throw new ServiceException(ErrorCode.NO_AUTH);
        }

        Tag tag = tagMapper.selectById(id);
        if (tag == null){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        return tag;
    }

    @Override
    public List<Tag> searchTag(String tagName) {

        //if (!userService.isAdmin(request)){
        //    throw new ServiceException(ErrorCode.NO_AUTH);
        //}
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(tagName)){
            queryWrapper.lambda().like(Tag::getTagName, tagName);
        }
        return tagMapper.selectList(queryWrapper);

    }
}
